//
//  YWRepairRecordTableViewCell.m
//  maintain
//
//  Created by Kyo on 16/9/27.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import "YWRepairRecordTableViewCell.h"
#import "UIColor+CustomColors.h"
#import "YWMacro.h"
#import "Masonry.h"
#import "UILabel+AttributedText.h"
#import "YWCommonTool.h"

#import "YWHandler.h"
#import "YWEquipmentTroubleRecord.h"
#import "YWEquipment.h"
#import "YWEquipmentManager.h"
#import "YWBuildingManager.h"
#import "YWLocation.h"
#import "YWReportTroubleModel.h"
#import "YWContactManager.h"
#import "YWReplyManager.h"
#import "YWReply.h"

@interface YWRepairRecordTableViewCell()
/** 编号Label */
@property (weak, nonatomic) UILabel *codeLabel;
/** 维修人 */
@property (weak, nonatomic) UILabel *executorLabel;
/** 设备 */
@property (weak, nonatomic) UILabel *equipmentLabel;
/** 位置 */
@property (weak, nonatomic) UILabel *locationLabel;
/** 维修时间 */
@property (weak, nonatomic) UILabel *timeLabel;
/** 说明 */
@property (weak, nonatomic) UILabel *noteLabel;
@end

@implementation YWRepairRecordTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubviews];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}
- (void)setTroubleRecord:(YWEquipmentTroubleRecord *)troubleRecord{
    _troubleRecord = troubleRecord;
    [_codeLabel labelText:@"编号: " withAttributedText:troubleRecord.recordSequence?:@"无" andAttributedColor:[UIColor customYellowColor]];
    
    [_executorLabel labelText:@"执行人: " withAttributedText:troubleRecord.handler.userName?:@"无" andAttributedColor:[UIColor customYellowColor]];
    
    NSString *equipmentName = nil;
    YWEquipment* equipment = nil;
    if (troubleRecord.equipmentId) {
        equipment = [[YWEquipmentManager shareManager] equipmentWithId:troubleRecord.equipmentId];
        equipmentName = equipment.equipmentName;
    }else if (troubleRecord.equipmentTypeId){
        equipmentName = [[YWEquipmentManager shareManager] equipmentTypeNameWithTypeId:troubleRecord.equipmentTypeId];
    }
    [_equipmentLabel labelText:@"设备: " withAttributedText:equipmentName.length?equipmentName:@"无" andAttributedColor:[UIColor blackColor]];
    
    NSString *location = nil;
    if (equipment) {
        location = [[YWBuildingManager shareManager] equipmentLocation:equipment];
    }else{
        location = [[YWBuildingManager shareManager]areaLocationWithLocationId:troubleRecord.trouble.location.locationId];
    }
    [_locationLabel labelText:@"位置: " withAttributedText:location.length?location:@"无" andAttributedColor:[UIColor blackColor]];
    
    NSString *time = [YWCommonTool showFullTime:troubleRecord.createTime/1000];
    [_timeLabel labelText:@"维修时间: " withAttributedText:time.length?time:@"无" andAttributedColor:[UIColor customYellowColor]];
    
    [_noteLabel labelText:@"备注: " withAttributedText:troubleRecord.note?:@"无" andAttributedColor:[UIColor blackColor]];
    //任务参与人，要把报障人算进去
    NSInteger showCount = 1;
    NSInteger observerCount = _troubleRecord.trouble.observers.count;
    NSMutableArray * observerMur = [NSMutableArray arrayWithArray:_troubleRecord.trouble.observers];//将参与者拿出来
    NSMutableArray * observerIds = [NSMutableArray array];
    [observerMur enumerateObjectsUsingBlock:^(YWHandler* _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        [observerIds addObject:@(handler.uniqueId)];
    }];
    if (observerCount) {
        NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"%K = %ld", @"uniqueId", _troubleRecord.trouble.reporter.uniqueId];
        NSArray *array = [[_troubleRecord.trouble.observers filteredArrayUsingPredicate:searchPredicate] mutableCopy];
        if (array.count==1) {
            observerCount--;
            [observerIds removeObject:@(_troubleRecord.trouble.reporter.uniqueId)];
        }
        showCount = showCount + observerCount;
    }
    NSString * firstStr = [NSString stringWithFormat:@"参与人(%ld): ",(long)showCount];
    
    [observerMur removeAllObjects];
    NSMutableString *observerName = [[NSMutableString alloc] init];
    if (observerIds.count) {
        for (id obj in observerIds ) {
            NSInteger uniqueId = [obj integerValue];
            YWHandler *handler = [[YWContactManager shareManager] getHandlerWithUniqueId:uniqueId];
            [observerMur addObject:handler];
        }
    }
    [observerMur enumerateObjectsUsingBlock:^(YWHandler*  _Nonnull handler, NSUInteger idx, BOOL * _Nonnull stop) {
        if (observerName.length && idx!=0) {
            [observerName appendString:@" "];
        }
        [observerName appendString:handler.userName];
    }];
    
    NSString * nameStr = [NSString stringWithFormat:@"%@ %@",_troubleRecord.reporterName?:@"",troubleRecord.handler.userName?:@""];//参与者要加进来
    
    self.observersLabel.attributedText = [YWCommonTool attributedTextWithFirstText:firstStr firstColor:[UIColor customTextGrayBlackColor] secondText:nameStr secondColor:[UIColor customBlueColor] font:kContentFont];
    //有评论
    NSArray *commentArray = [[YWReplyManager alloc] replysWithtRelationId:_troubleRecord.troubleId andRelationType:YWReplyRelationTypeTrouble];
    if (commentArray.count>0) {
        YWReply * reply = [commentArray firstObject];
        NSString * replyCount = [NSString stringWithFormat:@"评论(%ld): ",commentArray.count];
        //    时间
        NSString * timeStr = [YWCommonTool recordDateByFormatTimeString:reply.replyTime/1000 showYear:YES];
        //    内容
        
        NSString *atUserName = reply.atUser.userName.length>0?[NSString stringWithFormat:@" @%@",reply.atUser.userName]:@"";
        NSString * content = atUserName.length?[NSString stringWithFormat:@" %@  ",reply.content.length>0?reply.content:@""] : (reply.content.length>0?reply.content:@"");
        
        self.replyLabel.attributedText = [YWCommonTool attributedTextWithFirstText:replyCount firstColor:[UIColor customTextGrayColor] firstFont:kContentFont secondText:reply.replier.userName secondColor:[UIColor customBlueColor] secFont:kContentFont thirdText:atUserName thirdColor:[UIColor customBlueColor] thirdFont:kContentFont fourthText:content fourthColor:[UIColor blackColor] fourFont:kContentFont fifthText:timeStr fifthColor:[UIColor customTextGrayColor] fifthFont:[UIFont systemFontOfSize:13]];
    }else{
        self.replyLabel.attributedText = nil;
    }

    _locationLabel.preferredMaxLayoutWidth = _locationLabel.frame.size.width;
    _noteLabel.preferredMaxLayoutWidth = _noteLabel.frame.size.width;
    self.observersLabel.preferredMaxLayoutWidth = self.observersLabel.frame.size.width;
    self.replyLabel.preferredMaxLayoutWidth = self.replyLabel.frame.size.width;
}

- (void)setUpSubviews {
    
    UIView *superview = self.contentView;
    [self.codeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superview.mas_top).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    [self.executorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_codeLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    [self.equipmentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_executorLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_equipmentLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_locationLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
    }];
    
    [self.noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_timeLabel.mas_bottom).offset(6);
        make.left.mas_equalTo(superview).offset(8);
        make.right.mas_equalTo(superview).offset(-8);
//        make.bottom.mas_equalTo(superview.mas_bottom).offset(-8);
    }];
    [self.separatorLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_noteLabel.mas_bottom).offset(6);
        make.left.and.right.mas_equalTo(superview);
        make.height.mas_equalTo(@1.0);
    }];
    
    [self.observersLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.separatorLineView.mas_bottom).offset(6);
        make.left.and.right.mas_equalTo(_noteLabel);
    }];
    [self.replyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.observersLabel.mas_bottom).offset(6);
        make.left.and.right.mas_equalTo(_noteLabel);
        make.bottom.mas_equalTo(superview).offset(-6);
    }];
}

-(UILabel *)codeLabel{
    if (!_codeLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _codeLabel = label;
        [self.contentView addSubview:_codeLabel];
    }
    return _codeLabel;
}

-(UILabel *)executorLabel{
    if (!_executorLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _executorLabel = label;
        [self.contentView addSubview:_executorLabel];
    }
    return _executorLabel;
}

- (UILabel *)equipmentLabel{
    if (!_equipmentLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _equipmentLabel = label;
        [self.contentView addSubview:_equipmentLabel];
    }
    return _equipmentLabel;
}

- (UILabel *)locationLabel{
    if (!_locationLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor customTextGrayBlackColor];
        _locationLabel = label;
        [self.contentView addSubview:_locationLabel];
    }
    return _locationLabel;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.textColor = [UIColor customTextGrayBlackColor];
        _timeLabel = label;
        [self.contentView addSubview:_timeLabel];
    }
    return _timeLabel;
}

- (UILabel *)noteLabel{
    if (!_noteLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.font = kContentFont;
        label.numberOfLines = 0;
        label.textColor = [UIColor customTextGrayBlackColor];
        _noteLabel = label;
        [self.contentView addSubview:_noteLabel];
    }
    return _noteLabel;
}
@end
